//app.js
var Promise = require('utils/es6-promise.min.js');
var wxPromisify = require('utils/wxPromisify.js');
App({
  onLaunch: function () {
    var that = this;
    
    //获取屏幕信息
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.statusBarHeight = res.statusBarHeight
      }
    })
    //每次登录都会更新用户信息
    that.loginedToDo(that.authorize); 
  },
  //可以执行必须登录后才能做的函数,参数至多有一个,所以需要多个参数时需要传递对象
  loginedToDo : function(callback,data) {
    var that = this;
    var rdSession = wx.getStorageSync("rdSession");
    //未找不到rdSession表示一定未登录,则需要登录后才能执行
    if (!rdSession) {
      that.login(callback,data);
      return;
    //若找到rdSession则直接调用
    } else if (typeof (callback) == 'function') {
      that.globalData.rdSession = rdSession;
      callback(data);
    }  
  }
  ,
  login : function (callback,data) {
    var that = this;
    var wxLogin = wxPromisify.wxLogin();
    wxLogin().then(res=>{
      if (res.code) {
        var url = that.globalData.urlPath + "login.action";
        wxPromisify.getRequest(url, {"code": res.code}).then(res => {
          //登录成功
          if(res.data.status == 200) {
            console.log('登录成功！' + res.data);
            wx.setStorageSync("rdSession", res.data.data);
            that.globalData.rdSession = res.data.data;
            //登录成功后若有callback,则需要执行callback
            if (typeof(callback) ==  'function') {
              callback(data);
            }
          }else {
            console.log('登录失败！' + res.data.msg);
          }
        })
      } else {
        console.log('登录失败！' + res.errMsg)
      }
    })
  },
  //暂时不需要验证
  checkRdSession: function(rdSession) {
    var that = this;
    wxPromisify.getRequest(
      that.globalData.urlPath+"check.action",
      {
        rdSession: rdSession
      }
    ).then(res=>{
        console.log(res.data)
        //不等于200表示需要重新登录
        if (res.data.status != 200) {
          console.log("服务器的Session过期");
          that.login();
        }else {
          console.log("服务器Session未过期");
        }
    });
  }
  ,
  authorize: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称并更新到数据库
          console.log("已授权");
          wx.getUserInfo({
            success: function (res) {
              that.globalData.hasAuthorize = true;
              that.updateUserInfo(res.userInfo);
            }
          })
        }
      }
    })
  }
  ,
  //根据rdSession更新用户信息,若是由于登录过期引起更新失败则
  updateUserInfo: function (userInfo) {
    var that = this;
    var rdSession = that.globalData.rdSession;
    userInfo["rdSession"] = rdSession;   
    that.globalData.userInfo = userInfo;
    wx.request({
      url: that.globalData.urlPath + 'updateUserInfo.action',
      data: userInfo,
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      success: function (res) {    
        //返回500表示登录过期,需要重新登录,但也可以不更新信息,因为下次启动会自动更新信息
        if(res.data.status == 200) {
          console.log("更新用户信息成功");
        } else if (res.data.status == 500) {
          //需要重新登录且该函数还要继续执行,所以要继续传递
          that.login(that.updateUserInfo, userInfo);
        }
      }
    });
    

  },

  globalData: {
    urlPath:"http://120.77.36.131:8180/STULive/",
    imgPath: "http://120.77.36.131:8180/STULiveImg/",
    rdSession:"",
    hasAuthorize: false,
    userInfo: null,
    statusBarHeight:null,
    title_height: "64",
    statusbarHeight: "24",
    titleIcon_height: "32",
    titleIcon_width: "87",
    title_top: "24",
    title_text: "首页", // iphone X + 24     
    prefix: 24
  }
})